<template>
  <!-- 按钮 -->
  <div class="btn-config">
    <FooterButton
      :button-disable="btnOption"
      class="btn-init"
      @handleNext="handleClick('doNext')"
      @handleBack="handleClick('doBack')"
      @handleClose="handleClick('doClose')"
    />
    <!-- 下一步  -->
    <next-page
      v-if="nextPageVisible"
      ref="nextPage"
      :visible.sync="nextPageVisible"
      :activity-inst-id="workFlowData.activityInstID"
      :process-inst-id="workFlowData.processInstID"
      :activity-def-id="workFlowData.activityDefID"
      :process-def-name="workFlowData.processDefName"
      :prjDetail="prjDetail"
      :org-id="prjDetail.orgId"
      @doNextEntity="nextPageRes"
    />
    <!--  回退 -->
    <Back-page
      v-if="backPageVisible"
      ref="backPage"
      :visible.sync="backPageVisible"
      :process-inst-id="workFlowData.processInstID"
      :act-inset-id="workFlowData.activityInstID"
      @doReturn="backPageRes"
    />
  </div>
</template>

<script>
import FooterButton from '@/components/footer/details'
import NextPage from '@/components/nextPerson/NextPage.vue'
import BackPage from '@/components/nextPerson/BackPage'
import {doNextSwapLabelFLow, getUserInfo, insertOuterResourceReplaceLabelLog,getResourceReplaceLabelLog} from "@/api/material";
import {clonePage, enableScroll, msgInfoEl} from "@/utils";
import {getBackActivities} from "@/api/workFlow";
import {batchDeleteApi} from "@/api/attachmentInfo";
export default {
    name: 'ButtonProcess',
    components: {
        NextPage,
        BackPage,
        FooterButton
    },
    props: {
        workFlowData: {
            type: Object,
            default: () => ({ })
        },
        prjDetail: {
            type: Object,
            default: () => ({ })
        },
        isEdit:{
            type:Boolean,
            default:false
        }
    },
    data() {
        return {
            btnOption: ['doNext', 'back', 'close'],
            nextPageVisible: false, // 下一步弹框显示
            backPageVisible: false, // 回退弹框显示
        }
    },
    methods: {
        // 按钮回调方法
        async handleClick(fun) {
          console.log(fun)
            this[fun]()
        },
        checkSubmit(){
            if(!this.prjDetail.reason){
                msgInfoEl({
                    type:'info',
                    message:'请选择换签原因'
                })
                return true;
            }
            if(!this.prjDetail.remark){
                msgInfoEl({
                    type:'info',
                    message:'请输入具体情况说明'
                })
                return true;
            }
            if(this.prjDetail.uploadImageList.length === 0){
                msgInfoEl({
                    type:'info',
                    message:'请上传图片'
                })
                return true;
            }
            return false;
        },
        // 下一步按钮主方法
        async doNext() {
            console.log(123,this.prjDetail)
            if(this.checkSubmit()){
                return;
            }
            // 赋值打开下一步弹框
            this.nextPageVisible = true
        },
        // 退回按钮主方法
        async doBack() {
            if(this.isEdit){
                //最后一步回退 直接调接口
                this.$confirm('请确认是否取消本次换签流程？确定后您申请的换签流程将被取消。', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    cancelButtonClass: 'pms-zr-button',
                    confirmButtonClass: 'pms-zr-button-primary',
                    closeOnClickModal: false
                }).then(() => {
                    const ifNextVal = '2'
                    this.nextBackPageRes({}, ifNextVal)
                }).catch(() => {
                    console.log('取消删除了')
                })
            }else{
                this.backPageVisible = true
            }
        },
        // 关闭页面主方法
        doClose() {
            clonePage()
        },
        // 下一步确认回调
        nextPageRes(item) {
            const ifNextVal = '0' // 下一步传 0
            this.nextBackPageRes(item, ifNextVal)
        },
        // 退回确认回调
         backPageRes(item) {
            const ifNextVal = '1' // 回退传1
            this.nextBackPageRes(item, ifNextVal)
        },

        // 下一步 && 回退 &&转派 确认函数
        async nextBackPageRes(item, ifNextVal) {
            console.log(item, ifNextVal, 'ifNextVal')
            let params={}
            // 下一步
            if (ifNextVal === '0') {
                params={
                    workItemId: this.workFlowData.workItemID,
                    userIds: item.userId ? item.userId : item.empId,
                    route:"",
                    wfXpath:[],
                    opinion: item.opinion,
                    nextSceneId:item.sceneId,
                    nodeId:item.nodeId,
                    detailId: item?.detailId,
                    activityDefID: this.workFlowData.activityDefID,
                    templateId: item?.templateId,
                }
            } else if (ifNextVal === '1') {
                params={
                    workItemId:this.workFlowData.workItemID,
                    opinion: item.opinion,
                    detailId: item?.detailId,
                    nextActDefId:item.nodeId
                }
            } else if(ifNextVal === '2'){
                params={
                    processInstId:this.workFlowData.processInstID,
                }
            }else{
                console.log("阻断修复");
            }
            const data = {
                swapLabelFlowId: this.workFlowData.bizObject?.bizSeq,
                flowFlag: ifNextVal,
                reason:this.prjDetail.reason,
                remark:this.prjDetail.remark,
                uploadImageList:this.prjDetail.uploadImageList,
                swapStatus: ifNextVal === '2' ? 'C' : item.nodeId,
                wfNextDealParam:params,
            }
            const finalNextFlow = await doNextSwapLabelFLow(data)
            if (+finalNextFlow.code === 200) {
                // 关闭页面
                if(item.nodeId === 'finishActivity' && ifNextVal === '0'){
                    this.pushZy();
                }else{
                    this.doClose()
                }
            }
        },
        pushZy(){
            getUserInfo(this.workFlowData.bizObject?.bizSeq).then((res)=>{
                if(+res.code === 200){
                    const obj={
                        "resId":"",
                        "assetNo":this.prjDetail.resourceAssetCode,
                        "action":"2",
                        "roles":'',
                        "labelId":this.prjDetail.newLabelId,
                        "cityId":"",
                        "cityName":"",
                        "provinceId":"",
                        "provinceName":"",
                        "reason":this.prjDetail.reason,    //换签原因
                        "sysSource":"PMS",
                        "oldLabelId":this.prjDetail.oldLabelId,
                        "createLoginName":res.data.name,
                        "createOp":res.data.loginName,
                        "reasonDesc":this.prjDetail.remark, //具体情况说明
                        "longitude":this.prjDetail.longitude,
                        "latitude":this.prjDetail.latitude,
                    }
                    insertOuterResourceReplaceLabelLog(obj).then((result)=>{
                        if(result.resultStat === '000'){
                            const a={
                                assetNo:this.prjDetail.resourceAssetCode,
                                sysSource:""
                            }
                            getResourceReplaceLabelLog(a).then(()=>{
                                this.doClose()
                            })
                        }
                    })
                }
            })
        }
    }
}

</script>
<style lang="scss" scoped>
.btn-config {
    width: 100%;
    padding: 15px;
    // border: 1px solid #EBEEF5;
    display: flex;
    align-items: center;
    justify-content: center;

    .btn-init {
        width: 100%;
    }
}

.el-message--error {
    white-space: pre-line
}

</style>
